package com.whc.gulimall.order.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author angaoen
 */
@Configuration
public class GuliFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return template -> {
            // RequestAttributes接口的基于Servlet的实现。
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            // 获取到原请求
            HttpServletRequest request = attributes.getRequest();
            if (request != null) {
                String cookie = request.getHeader("Cookie");
                // 获取到我们的Cookie存放在feign请求模板中
                template.header("Cookie", cookie);
            }
        };
    }

}
